﻿//***************************************************
//
//       File Name:    ReadXmlManager.cs
//
//       Author:   王文军    Email:  546527816@qq.com
//
//
//***************************************************

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;

public static class ReadXmlManager
{

    public static string xmlValue;

    /// <summary>
    /// 读取XML
    /// </summary>
    /// <param name="xmlName">XML文件名</param>
    /// <param name="touJieDian">头结点名</param>
    /// <param name="jieDianName">子结属性值</param>
    /// <param name="infoName">再子结点名</param>
    /// <returns></returns>
    public static string OnReadXml(string xmlName, string touJieDian, string jieDianName, string infoName)
    {
        string filepath = Application.dataPath + "/StreamingAssets/xml/" + xmlName;
        if (File.Exists(filepath))
        {
            XmlDocument doc = new XmlDocument();
            XmlReaderSettings _set = new XmlReaderSettings();
            //忽略xml的注释
            _set.IgnoreComments = true;
            doc.Load(XmlReader.Create(filepath, _set));
            ////获取doc下面的第一个结点 SkillInfo
            //XmlNode skillInfoNode = doc.FirstChild;
            ////获取一个结点的所有子结点
            //XmlNodeList skillInfoList = skillInfoNode.ChildNodes;
            //获取doc下面指定结点的的所有子结点
            XmlNodeList skillInfoList = doc.SelectSingleNode(touJieDian).ChildNodes;
            //遍历所有的子结点
            foreach (XmlNode skillNode in skillInfoList)
            {
                //获取当前结点所有的属性
                XmlAttributeCollection attributeCol = skillNode.Attributes;
                XmlAttribute idAttribute = attributeCol["Name"];
                if (idAttribute.Value == jieDianName)
                {
                    //得到当前技能结点的所有子节点
                    XmlNodeList nodeList = skillNode.ChildNodes;
                    foreach (XmlNode node in nodeList)
                    {
                        //获取结点内部的文本字符串
                        if (node.Name == infoName)
                        {
                            xmlValue = node.InnerText;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        return xmlValue;
    }

    /// <summary>
    /// 更新XML
    /// </summary>
    /// <param name="xmlName">XML文件名</param>
    /// <param name="touJieDian">头结点名</param>
    /// <param name="jieDianName">子结属性值</param>
    /// <param name="infoName">再子结点名</param>
    /// <param name="infoValue">更新的值</param>
    /// <returns></returns>
    public static void UpdateXml(string xmlName, string touJieDian, string jieDianName, string infoName, string infoValue)
    {
        string filepath = Application.dataPath + "/StreamingAssets/xml/" + xmlName;
        if (File.Exists(filepath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            //根据路径将XML读取出来
            xmlDoc.Load(filepath);
            //得到transforms下的所有子节点
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(touJieDian).ChildNodes;
            //遍历所有子节点
            foreach (XmlElement xe in nodeList)
            {
                //拿到节点中属性ID =0的节点
                if (xe.GetAttribute("Name") == jieDianName)
                {
                    //    //更新节点属性
                    //    xe.SetAttribute("id", "1000");
                    //继续遍历
                    foreach (XmlElement x1 in xe.ChildNodes)
                    {
                        if (x1.Name == infoName)
                        {
                            //这里是修改节点名称对应的数值，而上面的拿到节点连带的属性。。。
                            x1.InnerText = infoValue;
                        }
                    }
                    break;
                }
            }
            xmlDoc.Save(filepath);
            Debug.Log("UpdateXml OK!");
            xmlDoc = null;
        }
    }
}
